package com.zgkxzx.android.androidtest;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;



/**
 * 创建者 zgkxzx
 * 日期   2/8/18
 * 描述
 */

public class LocalService extends NotificationListenerService {
    private static final String TAG = LocalService.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "==========onCreate==");

//        EventBus.getDefault().register(this);

        toggleNotificationListenerService();

    }


    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);

        Log.d(TAG, "===========================notify==" + sbn.getPackageName());

         EventBus.getDefault().post(new MsgEvent("StatusBarNotification-TAG", sbn));

    }

    private void toggleNotificationListenerService() {
        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName(this, LocalService.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        pm.setComponentEnabledSetting(new ComponentName(this, LocalService.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }


    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }
}
